package com.huiminpay.merchant.controller;

import com.huiminpay.common.cache.domain.BusinessException;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.common.cache.util.PhoneUtil;
import com.huiminpay.merchant.api.service.MerchantServiceApi;
import com.huiminpay.merchant.common.util.SecurityUtil;
import com.huiminpay.merchant.convert.MerchantRegisterConvert;
import com.huiminpay.merchant.api.dto.MerchantDTO;
import com.huiminpay.merchant.service.FileService;
import com.huiminpay.merchant.service.SmsService;
import com.huiminpay.merchant.vo.MerchantRegisterVO;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@Api(value = "商户平台‐商户相关", tags = "商户平台‐商户相关", description = "商户平台‐商户相关")
public class MerchantController {

    @Reference
    private MerchantServiceApi merchantService;
    @Autowired
    private SmsService smsService;
    @Autowired
    private FileService fileService;

    @ApiOperation(value = "获取验证码")
    @ApiImplicitParam(value = "手机号", name = "phone", required = true, dataType = "String", paramType = "query")
    @GetMapping("/sms")
    public String getSmsCode(@RequestParam("phone") String phone) {
        return smsService.getSmsCode(phone);
    }

    @ApiOperation(value = "根据id获取商户信息")
    @ApiImplicitParam(value = "商户ID", name = "id", required = true, dataType = "Long", paramType = "path")
    @GetMapping("/merchant/{id}")
    public MerchantDTO findMerchantById(@PathVariable("id") Long id) {
        return merchantService.findMerchantById(id);
    }

    @ApiOperation("注册商户")
    @ApiImplicitParam(name = "merchantRegisterVO", value = "注册信息", required = true, dataType =
            "MerchantRegisterVO", paramType = "body")
    @PostMapping("/merchants/register")
    public MerchantRegisterVO registerMerchant(@RequestBody MerchantRegisterVO merchantRegisterVO) {
        //校验参数的合法性
        if (merchantRegisterVO == null) {
            throw new BusinessException(CommonErrorCode.E_100108);
        }
        if (StringUtils.isBlank(merchantRegisterVO.getMobile())) {
            throw new BusinessException(CommonErrorCode.E_100112);
        }
        //手机号格式校验
        if (!PhoneUtil.isMatches(merchantRegisterVO.getMobile())) {
            throw new BusinessException(CommonErrorCode.E_100109);
        }
        //校验验证码
        smsService.checkVerifiyCode(merchantRegisterVO.getVerifiykey(), merchantRegisterVO.getVerifiyCode());
        //添加商户信息
        MerchantDTO merchantDTO = MerchantRegisterConvert.INSTANCE.vo2dto(merchantRegisterVO);
        merchantService.registerMerchant(merchantDTO);
        return merchantRegisterVO;
    }

    @ApiOperation("证件上传")
    @PostMapping("/upload")
    @ApiParam(value = "上传的文件", required = true)
    public String upload(@RequestParam("file") MultipartFile file) throws Exception {
        return fileService.upload(file);
    }


    @ApiOperation("商户资质申请")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "merchantdto", value = "商户认证资料", required = true, dataType = "MerchantDTO", paramType = "body")
    })
    @PostMapping("/my/merchants/save")
    public void saveMerchant(@RequestBody MerchantDTO merchantdto) {
//        Long merchantId= 1L; //Bearer eyJtZXJjaGFudElkIjoxMzM1ODk5MDEyNzIxOTg3NTg2LCJ1c2VyX25hbWUiOiIxMTEiLCJtb2JpbGUiOiIxMzgzODM4NDkwMCJ9
        Long merchantId = SecurityUtil.getMerchantId();
        merchantService.applyMerchant(merchantId, merchantdto);
    }


}
